﻿
using Furion.Schedule;

namespace GnpFine.Application.NmpaSystem;

/// <summary>
/// 清理日志作业任务
/// </summary>
[JobDetail("NmpaPurchJob", Description = "药品采购", GroupName = "default", Concurrent = false)]
[PeriodHours(8, TriggerId = "trigger_NmpaPurchJob", Description = "药品采购", MaxNumberOfRuns = 5, RunOnStart = true, StartNow = true)]
public class NmpaPurchJob : IJob
{
    private readonly ISqlSugarClient _db;
    public NmpaPurchJob(ISqlSugarClient db)
    {
        _db = db;
    }

    public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {

        Log.Information("\n-----------药品采购更新--任务开始----------");

       // 获取定时任务的参数
       var requestParam = context.JobDetail.GetProperty("sqlwhere").ToString().Trim();
       
       // 从金蝶服务器获取数据
       ResponseModel<STOCK> kDCusinvdata = KdCloudOpertionUtils.GetCusdata<STOCK>("GnFine.Yaojian.WebAPI.ServicesStub.GetCustPurch.ExecuteService,GnFine.Yaojian.WebAPI.ServicesStub", requestParam);
       
       // 更新数据库
       var _childDb = _db.AsTenant().GetConnectionScopeWithAttr<STOCK>();
       var a = _childDb.Ado.ExecuteCommand("delete  from [STOCK] ");
       var b = _childDb.AsTenant().InsertableWithAttr(kDCusinvdata.data).ExecuteCommand();
       
       Log.Information($"药品采购更新--任务结束:数据获取{kDCusinvdata.rowcount}条,数据插入{b}条，删除{a}条\n");

    }
}

